//
// Created by 11510 on 2024/7/10.
//
#include <sys/eventfd.h>
#include <unistd.h>
#include <pthread.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

// 创建一个 eventfd 文件描述符
int efd = eventfd(3, 0);

void* thread_func(void* arg) {
    uint64_t u;
    // 读取 eventfd 文件描述符，阻塞直到有数据可读
    read(efd, &u, sizeof(uint64_t));
    printf("Thread received event: %llu\n", (unsigned long long)u);
    return NULL;
}

int main() {
    pthread_t thread;
    // 创建一个线程
    pthread_create(&thread, NULL, thread_func, NULL);

    uint64_t u = 8;
    // 写入 eventfd 文件描述符，唤醒阻塞的线程
    write(efd, &u, sizeof(uint64_t));

    // 等待线程结束
    pthread_join(thread, NULL);

    // 关闭 eventfd 文件描述符
    close(efd);

    return 0;
}
